package com.web.segment.matching.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;

import com.web.segment.matching.dictionary.DictionaryImpl;
  
/** 
 * @author Hades Guan 词典工具类 
 * @param <T>
 *  
 */  
public class DictionaryUtil<T> implements DictionaryImpl{  
    /** 
     * 从fileName文件中读入词典实例 
     *  
     * @param fileName 
     *            存储文件 
     * @return 词典实例 
     */  
    @SuppressWarnings("unchecked")  
    public T readDictionary(String fileName)  
    {  
        try  
        {  
            ObjectInputStream in = new ObjectInputStream(  
                    new BufferedInputStream(new FileInputStream(fileName)));  
            T dic = (T) in.readObject();  
            in.close();  
            return dic;  
        }  
        catch (Exception e)  
        {  
            System.err.println(e.getMessage());  
            return null;  
        }  
    }  
  
    /** 
     * 将词典实例dic写入到fileName文件中 
     *  
     * @param dic 
     *            词典实例 
     * @param fileName 
     *            存储文件 
     * @return 操作成功与否 
     */  
    public boolean writeDictionary(T dic, String fileName)  
    {  
        try  
        {  
            ObjectOutputStream out = new ObjectOutputStream(  
                    new BufferedOutputStream(new FileOutputStream(fileName)));  
            out.writeObject(dic);  
            out.flush();  
            out.close();  
            return true;  
        }  
        catch (IOException e)  
        {  
            System.err.println(e.getMessage());  
            return false;  
        }  
  
    }

	public void deleteWord(String word) {
		// TODO Auto-generated method stub
		
	}

	public void insertWord(String word) {
		// TODO Auto-generated method stub
		
	}

	public void loadDictionary(String fileName) {
		// TODO Auto-generated method stub
		
	}

	public boolean match(String word) {
		// TODO Auto-generated method stub
		return false;
	}

	public void print(PrintStream out) {
		// TODO Auto-generated method stub
		
	}

}  